package com.redepronik.medisys.seguridad.aspect;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.redepronik.medisys.seguridad.dao.BitacoraDao;
import com.redepronik.medisys.seguridad.entity.Bitacora;

@Aspect
@Component
public class BitacoraAspect {

	@Autowired
	public BitacoraDao bitacoraDao;

	Object object;

	// @AfterReturning("execution(* com.redepronik.negosys.invfac.service.*.*(..))")
	public void logBefore(JoinPoint joinPoint) throws IOException {

		object = joinPoint.getArgs();

		ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(byteArray);
		oos.writeObject(object);

		Calendar calendar = GregorianCalendar.getInstance();

		Bitacora bitacora = new Bitacora();
		bitacora.setUsuarioid(1);
		bitacora.setFecha(calendar.getTime());
		bitacora.setMetodo(joinPoint.getSignature().toString());
		bitacora.setObjeto(byteArray.toByteArray());
		bitacoraDao.insertar(bitacora);
	}
}
